package com.myc.train.business.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.myc.train.business.domain.TrainCarriage;
import com.myc.train.business.enums.SeatColEnum;
import com.myc.train.common.resp.PageResp;
import com.myc.train.common.utils.SnowUtil;
import com.myc.train.business.domain.DailyTrainCarriage;
import com.myc.train.business.domain.DailyTrainCarriageExample;
import com.myc.train.business.mapper.DailyTrainCarriageMapper;
import com.myc.train.business.req.DailyTrainCarriageQueryReq;
import com.myc.train.business.req.DailyTrainCarriageSaveReq;
import com.myc.train.business.resp.DailyTrainCarriageQueryResp;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class DailyTrainCarriageService {

    @Resource
    private DailyTrainCarriageMapper dailyTrainCarriageMapper;

    @Resource
    private TrainCarriageService trainCarriageService;

    private static final Logger LOG = LoggerFactory.getLogger(DailyTrainCarriageService.class);
    public void save(DailyTrainCarriageSaveReq req){
        DateTime now = DateTime.now();
        //自动计算出列数和座位数
        List<SeatColEnum> colsByType = SeatColEnum.getColsByType(req.getSeatType());
        req.setColCount(colsByType.size());
        req.setSeatCount(req.getRowCount() * req.getColCount());

        DailyTrainCarriage dailyTrainCarriage = BeanUtil.copyProperties(req, DailyTrainCarriage.class);

        if(ObjectUtil.isNull(dailyTrainCarriage.getId())){
            dailyTrainCarriage.setId(SnowUtil.getSnowflakeNextId());
            dailyTrainCarriage.setCreateTime(now);
            dailyTrainCarriage.setUpdateTime(now);
            dailyTrainCarriageMapper.insert(dailyTrainCarriage);
        }else{
            dailyTrainCarriage.setUpdateTime(now);
            dailyTrainCarriageMapper.updateByPrimaryKey(dailyTrainCarriage);
        }
    }

    public PageResp<DailyTrainCarriageQueryResp> queryList(DailyTrainCarriageQueryReq req){
       //定义查询条件
        DailyTrainCarriageExample dailyTrainCarriageExample = new DailyTrainCarriageExample();
        //根据id倒序排序
        dailyTrainCarriageExample.setOrderByClause("date desc , train_code asc , `index` asc");
        //创建查询对象
        DailyTrainCarriageExample.Criteria criteria = dailyTrainCarriageExample.createCriteria();
        //不为空则对其新增查询条件
        if(ObjectUtil.isNotEmpty(req.getDate())){
            criteria.andDateEqualTo(req.getDate());
        }
        if(ObjectUtil.isNotEmpty(req.getTrainCode())){
            criteria.andTrainCodeEqualTo(req.getTrainCode());
        }

        //新增分页信息
        PageHelper.startPage(req.getPage(),req.getSize());
        //根据条件查询结果
        List<DailyTrainCarriage> dailyTrainCarriages = dailyTrainCarriageMapper.selectByExample(dailyTrainCarriageExample);
        //new一个PageInfo并将结果放进来
        PageInfo<DailyTrainCarriage> dailyTrainCarriagePageInfo = new PageInfo<>(dailyTrainCarriages);
        //类拷贝
        List<DailyTrainCarriageQueryResp> queryResps = BeanUtil.copyToList(dailyTrainCarriages, DailyTrainCarriageQueryResp.class);
        //new一个PageResp
        PageResp<DailyTrainCarriageQueryResp> resp= new PageResp<>();
        //放数据
        resp.setTotal(dailyTrainCarriagePageInfo.getTotal());
        resp.setList(queryResps);


        return resp;
    }

    public void delete(Long id){
        dailyTrainCarriageMapper.deleteByPrimaryKey(id);
    }


    @Transactional
    public void genDailyTrain(Date date, String trainCode){
        DateTime now = DateTime.now();
        //删除当天的车厢信息
        DailyTrainCarriageExample dailyTrainCarriageExample = new DailyTrainCarriageExample();
        dailyTrainCarriageExample.createCriteria().andDateEqualTo(date).andTrainCodeEqualTo(trainCode);
        dailyTrainCarriageMapper.deleteByExample(dailyTrainCarriageExample);

        //获取车厢信息
        List<TrainCarriage> trainCarriages = trainCarriageService.selectCarriage(trainCode);
        //非空校验
        if(CollUtil.isEmpty(trainCarriages)){
            LOG.info("该车无车厢信息");
        }
        //有则新增
        for (TrainCarriage trainCarriage: trainCarriages){
            //将车厢的数据拷贝成当日的
            DailyTrainCarriage dailyTrainCarriage = BeanUtil.copyProperties(trainCarriage, DailyTrainCarriage.class);
            //传set
            dailyTrainCarriage.setId(SnowUtil.getSnowflakeNextId());
            dailyTrainCarriage.setCreateTime(now);
            dailyTrainCarriage.setUpdateTime(now);
            dailyTrainCarriage.setDate(date);
            dailyTrainCarriageMapper.insert(dailyTrainCarriage);
        }
    }

    //获取车厢列表
    public List<DailyTrainCarriage> selectBySeatType(Date date, String trainCode, String seatType){
        DailyTrainCarriageExample example = new DailyTrainCarriageExample();
        example.createCriteria().andDateEqualTo(date)
                .andTrainCodeEqualTo(trainCode)
                .andSeatTypeEqualTo(seatType);
       return dailyTrainCarriageMapper.selectByExample(example);
    }
}
